package  main.java.leetcode.editor.cn;
//2023-07-03 16:26:15
//三合一。描述如何只用一个数组来实现三个栈。 
//
// 你应该实现push(stackNum, value)、pop(stackNum)、isEmpty(stackNum)、peek(stackNum)方法。
//stackNum表示栈下标，value表示压入的值。 
//
// 构造函数会传入一个stackSize参数，代表每个栈的大小。 
//
// 示例1: 
//
// 
// 输入：
//["TripleInOne", "push", "push", "pop", "pop", "pop", "isEmpty"]
//[[1], [0, 1], [0, 2], [0], [0], [0], [0]]
// 输出：
//[null, null, null, 1, -1, -1, true]
//说明：当栈为空时`pop, peek`返回-1，当栈满时`push`不压入元素。
// 
//
// 示例2: 
//
// 
// 输入：
//["TripleInOne", "push", "push", "push", "pop", "pop", "pop", "peek"]
//[[2], [0, 1], [0, 2], [0, 3], [0], [0], [0], [0]]
// 输出：
//[null, null, null, null, 2, 1, -1, -1]
// 
//
// 
//
// 提示： 
//
// 
// 0 <= stackNum <= 2 
// 
// Related Topics 栈 设计 数组 👍 70 👎 0

class ThreeInOneLcci {
    public static void main(String[] args) {
        //创建该题目的对象方便调用
        TripleInOne solution = new ThreeInOneLcci().new TripleInOne(2);
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class TripleInOne {

        int N = 3;
        // 3 * n 的数组，每一行代表一个栈
        int[][] data;
        // 记录每个栈「待插入」位置
        int[] locations;

        public TripleInOne(int stackSize) {
            data = new int[N][stackSize];
            locations = new int[N];
        }

        public void push(int stackNum, int value) {
            int[] stk = data[stackNum];
            int loc = locations[stackNum];
            if (loc < stk.length) {
                stk[loc] = value;
                locations[stackNum]++;
            }
        }

        public int pop(int stackNum) {
            int[] stk = data[stackNum];
            int loc = locations[stackNum];
            if (loc > 0) {
                int val = stk[loc - 1];
                locations[stackNum]--;
                return val;
            } else {
                return -1;
            }
        }

        public int peek(int stackNum) {
            int[] stk = data[stackNum];
            int loc = locations[stackNum];
            if (loc > 0) {
                return stk[loc - 1];
            } else {
                return -1;
            }
        }

        public boolean isEmpty(int stackNum) {
            return locations[stackNum] == 0;
        }
    }

/**
 * Your TripleInOne object will be instantiated and called as such:
 * TripleInOne obj = new TripleInOne(stackSize);
 * obj.push(stackNum,value);
 * int param_2 = obj.pop(stackNum);
 * int param_3 = obj.peek(stackNum);
 * boolean param_4 = obj.isEmpty(stackNum);
 */
//leetcode submit region end(Prohibit modification and deletion)

}
